/**
 * @description 作品 数据操作
 */
const NavModel = require("../models/NavModel");
let mongoose = require("mongoose");
/**
 * 查询 导航栏 列表
 */
async function findNavListService() {
  const navs = await NavModel.find().lean().exec();
  let _id;
  let map = new Map();
  navs.forEach((item) => {
    _id = mongoose.Types.ObjectId(item._id).toString();
    if (!item.pid) {
      map.set(_id, item);
    }
  });

  navs
    .filter((item) => item.pid)
    .forEach((item) => {
      let m = map.get(item.pid);
      map.set(item.pid, {
        ...m,
        children: m.children ? [...m.children, item] : [item],
      });
    });
  return map.values();
}

module.exports = {
  findNavListService,
};
